const app = getApp();
const https = require('../../utils/https');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    point_list: [],
    check_item_id: 0,
    house_id: 0,
    loading: false,
    disabled: false
  },
  switchTab(obj) {
    var id = obj.currentTarget.dataset.id,
      value = obj.detail.value;
    this.setData({
      check_point_id: id,
      check_value: value
    })
    this.chanageCheckResult();
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this,
      is_login_inspector = wx.getStorageSync('is_login_inspector');
    if (!is_login_inspector || is_login_inspector != 1) {
      wx.showToast({
        title: '请先登陆验房师账号',
        icon: 'none',
        duration: 1500
      })
      wx.navigateBack({
        delta: 0,
      })
      return false;
    }
    if (options && options.check_item_id != '') {
      that.setData({
        check_item_id: options.check_item_id,
        house_id: options.house_id
      })
    }
  },
  /**
   * @xk 更新验房点位状态
   * @param {} obj 
   */
  chanageCheckResult: function (obj) {
    var that = this,
      check_point_id = this.data.check_point_id,
      house_id = this.data.house_id,
      check_items_id = this.data.check_item_id,
      check_value = this.data.check_value;
    https.post('saveCheckPoints', {
      check_items_id: check_items_id,
      check_point_id: check_point_id,
      house_id: house_id,
      check_value: check_value
    }, true).then(res => {
      if (res) {
        that.setData({
          check_point_result_id: res
        })
        that.getCheckPoint();
      }
    })
  },
  /**
   * @xk 提交验房结果
   */
  saveResult: function () {
    var that = this,
      check_item_id = this.data.check_item_id,
      house_id = this.data.house_id;
      wx.showModal({
        title: '环节确认',
        content: '您确定该环节全部验收完毕？',
        success: function (sm) {
          if (sm.confirm) {
              // 用户点击了确定 可以调用删除方法了
              that.setData({
                loading: true,
                disabled: true
              })
              https.post('saveCheckPointResult', {
                check_item_id: check_item_id,
                house_id: house_id
              }, false, '正在提交验房结果').then(res => {
                wx.navigateBack({
                  delta: 0,
                })
              })
            } else if (sm.cancel) {
              console.log('用户点击取消')
            }
          }
        })
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getCheckPoint();
  },
  /**
   * @xk 获取验房点位
   */
  getCheckPoint: function () {
    var that = this,
      house_id = this.data.house_id,
      check_item_id = this.data.check_item_id;
    https.post('getCheckPoint', {
      check_item_id: check_item_id,
      house_id: house_id
    }, true, '加载中').then(res => {
      that.setData({
        point_list: res.point_list
      })
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.onShow();
  },

})